using CfgTable;
using IQIGame.Onigao.Framework;
using IQIGame.Onigao.Game;

namespace IQIGame.Onigao.GamePlay
{
    public class UIHeroEvolutionPop : UIBaseWindow
    {
        #region Template Generate,don't modify
        protected class UIB_UIHeroEvolutionPop
        {
            #region ObjectBinding Generate 
            public IQIGame.Onigao.Framework.ExButton btnClose { protected set; get; }
            public IQIGame.Onigao.Framework.ExImage imageStar { protected set; get; }
            public UnityEngine.GameObject starCurrCell { protected set; get; }
            public UnityEngine.GameObject starOldCell { protected set; get; }
            public UnityEngine.GameObject feature { protected set; get; }
            public IQIGame.Onigao.Framework.ExText featureName { protected set; get; }
            public IQIGame.Onigao.Framework.ExText featureDesc { protected set; get; }
            public UnityEngine.GameObject attributeMouldGrowth { protected set; get; }
            public UnityEngine.GameObject attributeMouldBase { protected set; get; }
            public virtual void InitBinding(ObjectBinding __binding)
            {
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExButton>("btnClose", out var __tbv0);
                this.btnClose = __tbv0;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExImage>("imageStar", out var __tbv1);
                this.imageStar = __tbv1;
                __binding.TryGetVariableValue<UnityEngine.GameObject>("starCurrCell", out var __tbv2);
                this.starCurrCell = __tbv2;
                __binding.TryGetVariableValue<UnityEngine.GameObject>("starOldCell", out var __tbv3);
                this.starOldCell = __tbv3;
                __binding.TryGetVariableValue<UnityEngine.GameObject>("feature", out var __tbv4);
                this.feature = __tbv4;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExText>("featureName", out var __tbv5);
                this.featureName = __tbv5;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExText>("featureDesc", out var __tbv6);
                this.featureDesc = __tbv6;
                __binding.TryGetVariableValue<UnityEngine.GameObject>("attributeMouldGrowth", out var __tbv7);
                this.attributeMouldGrowth = __tbv7;
                __binding.TryGetVariableValue<UnityEngine.GameObject>("attributeMouldBase", out var __tbv8);
                this.attributeMouldBase = __tbv8;
            }
            #endregion ObjectBinding Generate 
        }
        #endregion Template Generate,don't modify

        #region Window Config
        /// <summary>
        /// 静态配置
        /// </summary>
        private static UICreateConfig _createConfig = new()
        {
            prefabName = nameof(UIHeroEvolutionPop),
            layer = EUILayer.Pop,
        };

        /// <summary>
        /// 创建UI的配置
        /// </summary>
        public override IUICreateConfig createConfig => _createConfig;
        #endregion Window Config

        #region fields
        private ListScrollAdapter<UIN_CommonAttribute> baseAttrs = null;
        private ListScrollAdapter<UIN_CommonAttribute> growthAttrs = null;
        private ListScrollAdapter<UIN_CommonDarkLight> oldStars = null;
        private ListScrollAdapter<UIN_CommonDarkLight> currStars = null;

        private HeroData heroData;
        private CfgHeroStar cfgHeroStar;
        private CfgHeroStar lastCfgHeroStar;

        #endregion fields

        #region properties

        protected UIB_UIHeroEvolutionPop ui { get; set; }
        protected UIMsgDispatcher uiMsgDispatcher => this._msgDispatcher as UIMsgDispatcher;

        #endregion properties

        protected override void BeforeInit()
        {
            ui = new UIB_UIHeroEvolutionPop();
            ui.InitBinding(this.csObjBind);
        }

        protected override void OnInit()
        {
            this.baseAttrs = this.InitListScroll<UIN_CommonAttribute>(this.ui.attributeMouldBase, this.OnAttrChanged);
            this.growthAttrs = this.InitListScroll<UIN_CommonAttribute>(this.ui.attributeMouldGrowth, this.OnGrowthAttrChanged);
            this.oldStars = this.InitListScroll<UIN_CommonDarkLight>(this.ui.starOldCell, this.OnOldStarChanged);
            this.currStars = this.InitListScroll<UIN_CommonDarkLight>(this.ui.starCurrCell, this.OnCurrStarChanged);
        }

        protected override void OnShow(UIBaseData data)
        {
            var d = data as UD_HeroEvolutionPop;
            this.SetData(d.heroData);
            this.ShowView();
        }

        protected override void OnHide()
        {

        }

        /// <summary>
        /// 添加UGUI侦听
        /// </summary>
        protected override void AddUGUIListener()
        {
            this.ui.btnClose.onClick.AddListener(this.OnClickBtnClose);
        }

        /// <summary>
        /// 添加全局侦听（MsgDispatcher）
        /// </summary>
        protected override void AddMsgListeners()
        {

        }

        protected override void OnDispose()
        {
            this.ClearData();
        }

        #region methonds

        private void OnClickBtnClose()
        {
            this.Close();
        }

        private void ClearData()
        {
            this.heroData = null;
            this.cfgHeroStar = null;
            this.lastCfgHeroStar = null;
        }

        private void SetData(HeroData data)
        {
            this.heroData = data;

            int heroCid = this.heroData.cid;
            int heroStar = this.heroData.star;
            this.cfgHeroStar = this.heroData.cfgStar;
            this.lastCfgHeroStar = TableCenter.heroStar.GetCfg(heroCid, heroStar - 1);
        }

        private void ShowView()
        {
            this.ShowStarIcon();
            this.ShowOldStars();
            this.ShowCurrStars();
            this.ShowFeature();
            this.ShowBaseAttributes();
            this.ShowGrouthAttributes();
        }

        private void ShowStarIcon()
        {
            // TODO 显示星图Icon
        }

        private void ShowOldStars()
        {
            this.oldStars.RefillCells(APIUIConst.HeroDefautMaxStar);
        }

        private void OnOldStarChanged(UIN_CommonDarkLight item, int index)
        {
            int nCurr = index;
            int bigStar = this.lastCfgHeroStar.BigStar - 1;
            bool isLight = (bigStar >= nCurr);
            item.Show(isLight);
        }

        private void ShowCurrStars()
        {
            this.currStars.RefillCells(APIUIConst.HeroDefautMaxStar);
        }

        private void OnCurrStarChanged(UIN_CommonDarkLight item, int index)
        {
            int nCurr = index;
            int bigStar = this.cfgHeroStar.BigStar - 1;
            bool isLight = (bigStar >= nCurr);
            item.Show(isLight);
        }

        private void ShowFeature()
        {
            bool isShow = this.cfgHeroStar.IsShowFeature;
            if (isShow)
            {
                this.ui.featureName.text = this.cfgHeroStar.SkillName;
                this.ui.featureDesc.text = this.cfgHeroStar.SkillEffectText;
            }
            this.ui.feature.SetActive(isShow);
        }

        private void ShowBaseAttributes()
        {
            var cfgStar = this.cfgHeroStar;
            this.baseAttrs.RefillCells(cfgStar.AttTypes.Count);
        }

        private void OnAttrChanged(UIN_CommonAttribute item, int index)
        {
            var cfgStar = this.cfgHeroStar;
            int attrCid = cfgStar.AttTypes[index];
            int attrValue = this.heroData.GetAttr(attrCid);
            int currValue = cfgStar.AttValues[index];
            int currGrowth = cfgStar.DeltaAttrValues[index];
            int lastValue = this.lastCfgHeroStar.AttValues[index];
            int lastGrowth = this.lastCfgHeroStar.DeltaAttrValues[index];
            int changNum = (currValue - lastValue) + (currGrowth - lastGrowth) * this.heroData.level;

            int showValue = attrValue - changNum;
            item.ShowWhite(attrCid, showValue);
            int colorIndex = -1;
            if (showValue < attrValue)
                colorIndex = 1;
            else if (showValue > attrValue)
                colorIndex = 0;
            item.ShowCompare(attrValue, colorIndex);
        }


        private void ShowGrouthAttributes()
        {
            var cfgStar = this.cfgHeroStar;
            this.growthAttrs.RefillCells(cfgStar.AttTypes.Count);
        }

        private void OnGrowthAttrChanged(UIN_CommonAttribute item, int index)
        {
            var cfgStar = this.cfgHeroStar;
            int attrCid = cfgStar.AttTypes[index];
            int lastGrowth = this.lastCfgHeroStar.DeltaAttrValues[index];
            int currGrowth = cfgStar.DeltaAttrValues[index];
            item.ShowWhite(attrCid, lastGrowth);
            int colorIndex = -1;
            if (lastGrowth < currGrowth)
                colorIndex = 1;
            else if (lastGrowth > currGrowth)
                colorIndex = 0;
            item.ShowCompare(currGrowth, colorIndex);
        }

        #endregion methonds
    }
}
